<template>
  <div>
    <FormPanel  ref="formPanel"
        :list="initForm"   
        :cols="1"          
        :labelWidth="100"  
        :isSubmitBtn="false"  
        @formChange="formChangeHandle"
        class="mtop"
     />
     <div class="dialogBottmView">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button @click="saveDialog" :loading="saveLoading" type="primary">保 存</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { searchConstructionList } from '@/api/cms-api/origin/baseSetting/construction';
import { insertConstruction ,updataConstruction } from '@/api/cms-api/origin/baseSetting/constructionChilren';

import { getAppId } from '@/assets/js/auth';//获取登录用户
import { isArcfox } from '@/api/cms-api/origin/commonDealerType';		//渠道类型工具类
export default {
  props: {
    value:{
        type: Object,
        default: null
    }
  },
  mixins: [dictionary],
  data() {
    return {
      formData:[],
      constructionId:"",
      constructionList:[],  //项目列表
      saveLoading: false,
      initForm : this.initFormFun(),
      dealerTypeRemove:[] //渠道类型排除内容
    };
  },
  mounted() {
    
  },
  created() {
    //初始化页面中的渠道类型
    this.initType();
    //初始化项目下拉数据
    this.getConstructionData();
    
  },
  methods: {
    //初始化页面中的渠道类型
    initType(){
      //判断品牌，如果为ARCFOX时,渠道类型中，包含space、race
          if(isArcfox()){
            this.dealerTypeRemove = ['25381006'];
          }else{
            this.dealerTypeRemove = ['25381004','25381005','25381006'];//2538
          }
          this.initForm.find(x => x.fieldName === 'dealerTypeList').itemList = this.createDictListRemoveCodes('2538',this.dealerTypeRemove);
    },
    //修改时，初始化页面数据
    init(){
      //初始化ID
      this.constructionId =  this.value.rowData.constructionId;
      //转换经销商类型
      let dt =  this.value.rowData.dealerTypeStr.split(',') ;
      for(let i in dt){
        dt[i] = parseInt(dt[i]);
      }
      this.value.rowData.dealerTypeList = dt;
      //循环为表单赋值
      this.initForm.forEach(x => {
        x.initialValue = this.value.rowData[x.fieldName];
      });
    },
    //初始化项目下拉数据
    async getConstructionData() {
      const res = await searchConstructionList();
      if (res.resultCode === 200) {
        this.initForm[0].itemList = res.data.map(x => ({ value: parseInt(x.constructionId), text: x.constructionName }));
        this.constructionList = res.data;
        //如果传入参数不为空并且为修改
        if(this.value!=null && this.value.type=="edit"){
          this.init();
        }else{
          //默认设为启用状态
          this.initForm.find(x => x.fieldName === 'status').initialValue = 10031001;
        }
      }
    },
    //项目选择，级联经销商类型
    changeConstructSelect(val) {
      if (!val) {
        this.initForm[1].initialValue = [];
      } else {
        for(let i in this.constructionList){
          if(this.constructionList[i].constructionId == val){
              let dealerType = this.constructionList[i].dealerTypeStr.split(',');
              let dealerTypeArray = [];
              for(let j in dealerType){
                dealerTypeArray.push(parseInt(dealerType[j]));
              }
              this.initForm[1].initialValue =dealerTypeArray;
          }
        }
      }
    },
    //搜索数据
    initFormFun(){
        return [
          {
            type: 'SELECT',
            label: '项目名称',
            fieldName: 'parConstructionId',
            placeholder: '请选择',
            itemList: [],
            change: val => {
              this.changeConstructSelect(val);
            },
            rules: [{
              required: true,
              message: '必填',
              trigger: 'change'
            }]
          },
          {
            type: 'MULTIPLE_CHECKBOX',
            label: '渠道类型',
            fieldName: 'dealerTypeList',
            initialValue: [],
            disabled:true,
            itemList: [], //复选框数据源
            rules: [
              {
                required: true,
                message: '必填',
                trigger: 'change'
              }
            ]
          },
          {
            type: 'INPUT',
            label: '子项目代码',
            fieldName: 'constructionCode',
            placeholder: '请输入',
            initialValue: '系统自动生成',
            disabled:true
          },
          { 
            type: 'INPUT', 
            label: '子项目名称', 
            fieldName: 'constructionName', 
            placeholder: '请输入', 
            initialValue: '' ,
            rules: [
              {
                required: true,
                message: '必填',
                trigger: 'change'
              },
              { max: 300, 
                message: '长度最大300字符', 
                trigger: 'blur' 
              }
            ]
          },
          {
            type: 'SELECT',
            label: '状态',
            fieldName: 'status',
            placeholder: '请选择',
            initialValue: "",
            itemList: this.createDictList('1003'),
            rules: [{
              required: true,
              message: '必填',
              trigger: 'change'
            }]
          }
          ]
    },
    formChangeHandle(val){
      this.formData = val;
      this.doSaveDialog();
    },
    //保存按钮
    saveDialog(){
      this.$refs.formPanel.SUBMIT_FORM();
    },
    //执行保存
    async doSaveDialog(val){
      this.saveLoading = true;
      let res;
      //如果传入参数不为空并且为修改
      if(this.value!=null && this.value.type=="edit"){
        res = await updataConstruction(this.formData,this.constructionId);
      }else{
        res = await insertConstruction(this.formData);
      }
      if (res.resultCode === 200) {
        this.$notify.success({title: '提示信息',message:'保存成功!'});
        // 重新加载
        this.closeDialog('refresh');
      }else {
        this.$notify.error({title: '提示信息',message:res.errMsg||'保存失败!'});
        this.saveLoading = false;
        // this.closeDialog();
      }
    },
    //取消操作
    closeDialog(type){
      // this.$emit('close', {visible:false,});
      this.$emit('close', { visible: false,type:type});
    }
  },
}
</script>